package dLogistic.domain.model.Kurier;

import dLogistic.domain.model.BaseAggregateRoot;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.UUID;

/**
 * Created by Przemek on 2014-10-23.
 */
@Entity
@Table(name = "Kurier")
public class KurierAggr extends BaseAggregateRoot {
    private static SessionFactory factory;

    @Id
    private String uuid;        //uuid

    private String nazwa;


    public KurierAggr(String nazwa, String uuid){
        this.nazwa = nazwa;
        this.uuid = uuid;
    }

    public String uuid(){
        return uuid;
    }

    public String nazwa() {
        return nazwa;
    }

    public KurierAggr(){

    }

    public KurierTrasa KurierNowaTrasa(String MagStart, String MagEnd, String CzasStart, String CzasEnd, Integer dzien, String uuid_kurier, String uuid_trasa){
        try{
            factory = new Configuration().configure("/resources/hibernate.cfg.xml").buildSessionFactory();
        }catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        Session session = factory.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        if (uuid_trasa == "") {
            uuid_trasa = UUID.randomUUID().toString();
        }
        if (uuid_kurier == ""){
            uuid_kurier = this.uuid;
        }

        KurierTrasa kurierTrasa = new KurierTrasa(MagStart, MagEnd, CzasStart, CzasEnd, dzien, uuid_kurier, uuid_trasa);
        session.saveOrUpdate(kurierTrasa);
        transaction.commit();
        return kurierTrasa;
    }

    public void przyjeciePaczki(){

    }

    public void oddaniePaczki(){

    }



}
